package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import model.ClassPojo;
import model.ObjectPojo;

public class ObjectDao {

	// save objects

	public void insertObjectPojo(ObjectPojo objectpojo) {
		DBCon db = new DBCon();
		Connection con;
		try {
			con = db.getCon();

			// First check that an identical record doesn't already exist

			if (!doesObjectExist(objectpojo)) {
				Statement stmt;
				stmt = con.createStatement();
				stmt
						.executeUpdate("INSERT INTO objects(object_name, object_uri, object_ns) values('"
								+ objectpojo.getObjectName()
								+ "','"
								+ objectpojo.getObjectUri()
								+ "','"
								+ objectpojo.getNamespace() + "')");
			} else {
				Statement stmt;
				stmt = con.createStatement();
				stmt.executeUpdate("UPDATE objects SET object_name='"
						+ objectpojo.getObjectName() + "', object_uri='"
						+ objectpojo.getObjectUri() + "', object_ns='"
						+ objectpojo.getNamespace() + "' WHERE object_uri='"
						+ objectpojo.getObjectUri() + "'");
			}

		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public void saveObjectDefn(ObjectPojo opojo, ClassPojo cpojo) {

		// check that object exists
		boolean doesObjectExist = doesObjectExist(opojo);
		ClassDao cdao = new ClassDao();
		boolean doesClassExist = cdao.doesClassExist(cpojo);

		int objectId = -1;
		int classId = -1;
		if (doesObjectExist == false) {
			insertObjectPojo(opojo);
			objectId = getObjectId(opojo.getObjectUri());
		} else {
			objectId = getObjectId(opojo.getObjectUri());
		}

		if (doesClassExist == false) {
			cdao.insertClassPojo(cpojo);
			classId = cdao.getClassId(cpojo.getClassUri());
		} else {
			classId = cdao.getClassId(cpojo.getClassUri());
		}
		// now both the class and object exist
		
		try {
		//add the class_id
		DBCon db = new DBCon();
		Connection con = db.getCon();
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT object_def_id FROM object_defs WHERE object_id="+objectId+" AND class_id = "+classId+"");
		if(!rs.next()){
			// add the entry
			stmt.executeUpdate("INSERT INTO object_defs(object_id, class_id) VALUES("+objectId+","+classId+")");
		}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public boolean doesObjectExist(ObjectPojo objectpojo) {
		boolean doesItExist = true;

		DBCon db = new DBCon();
		Connection con;
		try {
			con = db.getCon();

			String objectName = objectpojo.getObjectName();
			String objectUri = objectpojo.getObjectUri();
			String objectNs = objectpojo.getNamespace();

			System.out.println("doesObjectExist: " + objectUri);

			Statement stmt;
			stmt = con.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT object_id FROM objects WHERE object_uri='"
							+ objectUri + "'");
			if (rs.next()) {
				doesItExist = true;
			} else {
				doesItExist = false;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return doesItExist;
	}

	public int getObjectId(String uri) {

		int id = -1;
		try {
			DBCon db = new DBCon();
			Connection con;

			con = db.getCon();

			Statement stmt;
			stmt = con.createStatement();
			ResultSet rs;
			rs = stmt
					.executeQuery("SELECT object_id FROM objects WHERE object_uri='"
							+ uri + "'");
			while (rs.next()) {
				id = rs.getInt("object_id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}

}
